package com.wangsh.usersCenter.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.usersCenter.common.pojo.ApiResponse;
import com.wangsh.usersCenter.common.pojo.ApiResponseEnum;
import com.wangsh.usersCenter.common.service.BaseServiceImpl;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.common.util.ExecutorServiceUtil;
import com.wangsh.usersCenter.common.util.PageInfoUtil;
import com.wangsh.usersCenter.system.pojo.ARegion;
import com.wangsh.usersCenter.system.pojo.ARegionEnum;
import com.wangsh.usersCenter.system.pojo.ASysIp;
import com.wangsh.usersCenter.system.pojo.ASysIpEnum;
import com.wangsh.usersCenter.system.service.ISystemDbService;
import com.wangsh.usersCenter.system.service.ISystemOperService;
import com.wangsh.usersCenter.util.ServiceCallableUtil;

/**
 * 操作的实现类
 * 
 * @author TeaBig
 */
 
@Service("systemOperService")
public class SystemOperServiceImpl extends BaseServiceImpl implements ISystemOperService
{
	@Resource
	private ISystemDbService systemDbService;

	@Override
	public ApiResponse<ASysIp> operFindIp(String ip) 
	{
		ApiResponse<ASysIp> apiResponse = new ApiResponse<ASysIp>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		condMap.put("ip", ip);
		try 
		{
			apiResponse = this.systemDbService.findOneSysIpService(condMap);
			// 查询这个ip数据 如果不等于null就说明在数据库查到了 ，直接返回输出
			if (apiResponse.getDataOneJava() == null) 
			{
				/**
				 **  如果数据库没查到 就要把这个ip的数据存到数据库
				 ** 先查询淘宝，如果淘宝没查到那就去ip138里面查
				 */
				Map<String, String> headerMap = new HashMap<String, String>();
				JSONObject responseJSON = this.jsoupUtil.ipLocation(ip, headerMap);
				/* 说明查询到数据了 */
				if(responseJSON != null && "0".equalsIgnoreCase(responseJSON.get("code") + ""))
				{
					JSONObject dataResJson = responseJSON.getJSONObject("data");
					ASysIp sysip = new ASysIp();
					if(!"".equalsIgnoreCase(dataResJson.getString("country_id")))
					{
						condMap.clear();
						condMap.put("engName", dataResJson.getString("country_id"));
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysip.setRegionId(region.getId());
						} 
					}else
					{
						/* 根据名字查询,国家 */
						condMap.clear();
						condMap.put("nameLike", "%" + dataResJson.getString("country") + "%");
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysip.setRegionId(region.getId());
							dataResJson.put("country_id", region.getPinyin());
						} 
						
					}
					/* 存储Ip */
					sysip.setName(dataResJson.getString("ip"));
					sysip.setIp(dataResJson.getString("ip"));
					sysip.setIpStr(dataResJson.getString("country") + "-" + dataResJson.getString("region") + "-"
							+ dataResJson.getString("city"));
					sysip.setIsp(dataResJson.getString("isp"));
					sysip.setResponse(responseJSON.toJSONString());
					sysip.setStatus(ASysIpEnum.STATUS_ENABLE.getStatus());
					sysip.setContent(dataResJson.getString("allInfo"));
					sysip.setPubTime(new Date());
	
					ApiResponse<Object> a = this.systemDbService.saveOneSysIpService(sysip);
	
					condMap.clear();
					condMap.put("ip", sysip.getIp());
					apiResponse = this.systemDbService.findOneSysIpService(condMap);
				}
			}
		} catch (Exception e) {
			ConstatFinalUtil.SYS_LOGGER.error("查询一条记录失败了", e);
			apiResponse.setCode(ApiResponseEnum.STATUS_SERVER_ERROR.getStatus());
			Map<String, String> infoMap = new HashMap<String, String>();
			// 报错信息的占位符
			infoMap.put("errinfo", e.toString());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), infoMap);			
		}
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operBatchRegionService()
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int totalCount = 0 ; 
		int succedCount = 0 ;
		int failedCount = 0 ; 
		Map<String, Object> dataMapJava = apiResponse.getDataMapJava() ;
		/* 判断条数 */
		dataMapJava.put("totalCount", totalCount);
		dataMapJava.put("succedCount", succedCount);
		dataMapJava.put("failedCount", failedCount);
		apiResponse.setDataMapJava(dataMapJava);
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 先存储中国 */
		condMap.put("areacode", "zh_CN");
		ApiResponse<ARegion> regionApiresponse = this.systemDbService.findOneRegionService(condMap);
		ARegion chinaRegion = regionApiresponse.getDataOneJava() ; 
		
		/* 多线程列表 */
		List<Callable<Object>> serviceCallableList = new ArrayList<Callable<Object>>();
		/* 抓取省份 */
		JSONObject provinceJSON = this.jsoupUtil.regionProvince();
		if(provinceJSON.getInteger("code") == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			JSONObject dataResJSON = (JSONObject) provinceJSON.get("data");
			/* 请求网址 */
			String requestUrl = dataResJSON.getString("requestUrl");
			/* 取出数据 */
			JSONArray regionArr = dataResJSON.getJSONArray("regionList");
			for (Iterator iterator = regionArr.iterator(); iterator.hasNext();)
			{
				JSONObject regionTempJSON = (JSONObject) iterator.next();
				String name = regionTempJSON.getString("name");
				String href = regionTempJSON.getString("href");
				String areacode = regionTempJSON.getString("areacode");
				String pinyin = this.characterUtil.chineseToPinyin(name);
				/* 创建地区 */
				ARegion regionTemp = new ARegion();
				regionTemp.setParentId(chinaRegion.getId());
				regionTemp.setName(name);
				regionTemp.setAreacode(areacode);
				regionTemp.setPinyin(pinyin);
				regionTemp.setContent(regionTemp.getName() + "_" + regionTemp.getAreacode());
				regionTemp.setLeafStatus(ARegionEnum.LEAFSTATUS_FALSE.getStatus());
				regionTemp.setStatus(ARegionEnum.STATUS_ENABLE.getStatus());

				regionTemp.setCreateTime(new Date());
				regionTemp.setUpdateTime(new Date());
				regionTemp.setPubTime(new Date());	
				/* 保存一条记录 */
				ApiResponse<Object> dbApiResponse = this.systemDbService.saveOneRegionService(regionTemp);
				// ConstatFinalUtil.SYS_LOGGER.info("插入region,结果:{}",dbApiResponse.toJSON().toJSONString());
				ConstatFinalUtil.TIMER_LOGGER.info("name:{},areacode:{},content:{}",regionTemp.getName() , regionTemp.getAreacode(),regionTemp.getContent());
				if(dbApiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
				{
					if(dataMapJava.get("succedCount") != null)
					{
						succedCount = Integer.valueOf(dataMapJava.get("succedCount") + "");
						succedCount ++ ;
						dataMapJava.put("succedCount", succedCount);
					}
				}else
				{
					if(dataMapJava.get("failedCount") != null)
					{
						failedCount = Integer.valueOf(dataMapJava.get("failedCount") + "");
						failedCount ++ ;
						dataMapJava.put("failedCount", failedCount);
					}
				}
				if(dataMapJava.get("totalCount") != null)
				{
					totalCount = Integer.valueOf(dataMapJava.get("totalCount") + "");
					totalCount ++ ;
					dataMapJava.put("totalCount", totalCount);
				}
				
				/* 存储其它城市的数据 */
				//this.operBatchRegionChildrenUtil(requestUrl, regionTempJSON, 1, regionTemp,dataMapJava);
				ServiceCallableUtil serviceCallableUtil = new ServiceCallableUtil();
				Map<String, Object> paramsMap = new HashMap<String, Object>();
				/* 设置参数 */
				paramsMap.put("systemOperService", this);
				paramsMap.put("requestUrl", requestUrl);
				paramsMap.put("regionJSON", regionTempJSON);
				paramsMap.put("region", regionTemp);
				paramsMap.put("dataMapJava", dataMapJava);
				serviceCallableUtil.setParamsMap(paramsMap);
				/* 类型 */
				serviceCallableUtil.setOperType(ServiceCallableUtil.OPERTYPE_BATCH_REGION);
				/* 将对象添加到多线程中 */
				serviceCallableList.add(serviceCallableUtil);
			}
			
			try
			{
				/* 执行多线程 */
				ExecutorServiceUtil.invokeAll(serviceCallableList, ExecutorServiceUtil.fiexNum);
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("多线程执行时失败了;",e);
			}
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		}
		return apiResponse;
	}
	
	/**
	 * 请求其它的城市化
	 */
	public void operBatchRegionChildrenUtil(String requestUrl,JSONObject regionJSON,int level,ARegion parentRegion,
			Map<String, Object> dataMapJava)
	{
		int totalCount = 0 ; 
		int succedCount = 0 ;
		int failedCount = 0 ; 
		/* 取到网址 */
		String href = regionJSON.getString("href");
		if(href != null && !"".equalsIgnoreCase(href))
		{
			/* 线程休眠 */
			ConstatFinalUtil.randSleep("slow");
			
			String urlStr = requestUrl + href ;
			/* 请求其它的地区码 */
			JSONObject resultJSON = this.jsoupUtil.regionOther(urlStr, level);
			
			if(resultJSON.getInteger("code") == ApiResponseEnum.STATUS_SUCCESS.getStatus())
			{
				JSONObject dataResJSON = (JSONObject) resultJSON.get("data");
				requestUrl = urlStr.substring(0, urlStr.lastIndexOf("/") + 1) ;
				/* 取出数据 */
				JSONArray regionArr = dataResJSON.getJSONArray("regionList");
				for (Iterator iterator = regionArr.iterator(); iterator.hasNext();)
				{
					JSONObject regionTempJSON = (JSONObject) iterator.next();
					String name = regionTempJSON.getString("name");
					String hrefRes = regionTempJSON.getString("href");
					String areacode = regionTempJSON.getString("areacode");
					String pinyin = this.characterUtil.chineseToPinyin(name);
					String townCode = regionTempJSON.getString("townCode");
					/* 创建地区 */
					ARegion regionTemp = new ARegion();
					regionTemp.setParentId(parentRegion.getId());
					regionTemp.setName(name);
					regionTemp.setAreacode(areacode);
					regionTemp.setPinyin(pinyin);
					regionTemp.setContent(regionTemp.getName() + "_" + regionTemp.getAreacode() + "_" + townCode);
					regionTemp.setLeafStatus(ARegionEnum.LEAFSTATUS_FALSE.getStatus());
					regionTemp.setStatus(ARegionEnum.STATUS_ENABLE.getStatus());
		
					regionTemp.setCreateTime(new Date());
					regionTemp.setUpdateTime(new Date());
					regionTemp.setPubTime(new Date());	
					/* 保存一条记录 */
					ApiResponse<Object> dbApiResponse = this.systemDbService.saveOneRegionService(regionTemp);
					// ConstatFinalUtil.SYS_LOGGER.info("插入region,结果:{}",dbApiResponse.toJSON().toJSONString());
					ConstatFinalUtil.TIMER_LOGGER.info("name:{},areacode:{},content:{}",regionTemp.getName() , regionTemp.getAreacode(),regionTemp.getContent());
					if(dbApiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
					{
						if(dataMapJava.get("succedCount") != null)
						{
							succedCount = Integer.valueOf(dataMapJava.get("succedCount") + "");
							succedCount ++ ;
							dataMapJava.put("succedCount", succedCount);
						}
					}else
					{
						if(dataMapJava.get("failedCount") != null)
						{
							failedCount = Integer.valueOf(dataMapJava.get("failedCount") + "");
							failedCount ++ ;
							dataMapJava.put("failedCount", failedCount);
						}
					}
					if(dataMapJava.get("totalCount") != null)
					{
						totalCount = Integer.valueOf(dataMapJava.get("totalCount") + "");
						totalCount ++ ;
						dataMapJava.put("totalCount", totalCount);
					}
					
					this.operBatchRegionChildrenUtil(requestUrl, regionTempJSON, level + 1, regionTemp,dataMapJava);
				}
			}
			
		}
	}

	@Override
	public ApiResponse<Object> operBatchCountryRegionService()
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int totalCount = 0 ; 
		int succedCount = 0 ;
		int failedCount = 0 ; 
		Map<String, Object> dataMapJava = apiResponse.getDataMapJava() ;
		/* 判断条数 */
		dataMapJava.put("totalCount", totalCount);
		dataMapJava.put("succedCount", succedCount);
		dataMapJava.put("failedCount", failedCount);
		apiResponse.setDataMapJava(dataMapJava);
		
		/* 获取所有的国家 */
		Locale[] locales = Locale.getAvailableLocales();
		for (int i = 0; i < locales.length; i++)
		{
			Locale locale = locales[i];
			/* 打印相关的信息 */
			/* 
			 * toString:hr_HR,getCountry:HR,getLanguage:hr,getDisplayCountry:克罗地亚,
			 * getDisplayLanguage:克罗地亚文,displayName:克罗地亚文 (克罗地亚),getDisplayScript:,
			 * getDisplayVariant:
			 *  */
//			ConstatFinalUtil.SYS_LOGGER.info("toString:{},getCountry:{},getLanguage:{},"
//					+ "getDisplayCountry:{},getDisplayLanguage:{},"
//					+ "displayName:{},getDisplayScript:{},"
//					+ "getDisplayVariant:{}",
//					locale,
//					locale.getCountry(),locale.getLanguage(),
//					locale.getDisplayCountry(),
//					locale.getDisplayLanguage(),
//					locale.getDisplayName(),locale.getDisplayScript(),
//					locale.getDisplayVariant());
			/* 国家简写 */
			String engName = locale.getCountry() ; 
			/* 名字 */
			String name = locale.getDisplayCountry() ;
			/* areacode */
			String areaCode = locale.toString() ; 
			if(engName != null && !"".equalsIgnoreCase(engName))
			{
				/* 先存储中国 */
				ARegion chinaRegion = new ARegion();
				chinaRegion.setName(name);
				chinaRegion.setPinyin(engName);
				chinaRegion.setAreacode(engName);
				chinaRegion.setContent(chinaRegion.getName() + "_" + chinaRegion.getAreacode());
				chinaRegion.setLeafStatus(ARegionEnum.LEAFSTATUS_FALSE.getStatus());
				chinaRegion.setStatus(ARegionEnum.STATUS_ENABLE.getStatus());
				chinaRegion.setCreateTime(new Date());
				chinaRegion.setUpdateTime(new Date());
				chinaRegion.setPubTime(new Date());	
				ApiResponse<Object> dbApiResponse = this.systemDbService.saveOneRegionService(chinaRegion);
				//ConstatFinalUtil.SYS_LOGGER.info("插入region,结果:{}",dbApiResponse.toJSON().toJSONString());
				if(dbApiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
				{
					if(dataMapJava.get("succedCount") != null)
					{
						succedCount = Integer.valueOf(dataMapJava.get("succedCount") + "");
						succedCount ++ ;
						dataMapJava.put("succedCount", succedCount);
					}
				}else
				{
					if(dataMapJava.get("failedCount") != null)
					{
						failedCount = Integer.valueOf(dataMapJava.get("failedCount") + "");
						failedCount ++ ;
						dataMapJava.put("failedCount", failedCount);
					}
				}
				if(dataMapJava.get("totalCount") != null)
				{
					totalCount = Integer.valueOf(dataMapJava.get("totalCount") + "");
					totalCount ++ ;
					dataMapJava.put("totalCount", totalCount);
				}
			}
		}
		
		/* 设置数据 */
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

	/**
	 * 	查询所有状态为禁用的ip,
	 * 	请求淘宝或者阿里,获取ip的相关信息
	 * 	更新ip的相关信息
	 */
	@Override
	public ApiResponse<Object> operBatchIpService()
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int totalCount = 0 ; 
		int succedCount = 0 ;
		int failedCount = 0 ; 
		Map<String, Object> dataMapJava = apiResponse.getDataMapJava() ;
		/* 判断条数 */
		dataMapJava.put("totalCount", totalCount);
		dataMapJava.put("succedCount", succedCount);
		dataMapJava.put("failedCount", failedCount);
		apiResponse.setDataMapJava(dataMapJava);
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/* 一次性处理100条 */
		pageInfoUtil.setPageSize(100);
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		/* 查询所有状态为禁用的ip, */
		condMap.put("status", ASysIpEnum.STATUS_DISABLE.getStatus() + "");
		this.systemDbService.findCondListSysIpService(pageInfoUtil, condMap);
		/* 循环一页一页的处理 */
		for (int i = 1; i <= pageInfoUtil.getTotalPage(); i++)
		{
			ConstatFinalUtil.SYS_LOGGER.info("批量处理Ip;总条数:{},总页数:{},当前页:{}",
					pageInfoUtil.getTotalRecord(),pageInfoUtil.getTotalPage(),pageInfoUtil.getCurrentPage());
			
			/* 重新查询一次 */
			condMap.clear();
			/* 查询所有状态为禁用的ip, */
			condMap.put("status", ASysIpEnum.STATUS_DISABLE.getStatus() + "");
			ApiResponse<ASysIp> sysIpApiResponse = this.systemDbService.findCondListSysIpService(pageInfoUtil, condMap);
			List<ASysIp> sysIpList = sysIpApiResponse.getDataListJava();
			for (Iterator iterator = sysIpList.iterator(); iterator.hasNext();)
			{
				ASysIp sysIp = (ASysIp) iterator.next();
				String ip = sysIp.getIp() ; 
				/**
				 **  如果数据库没查到 就要把这个ip的数据存到数据库
				 ** 先查询淘宝，如果淘宝没查到那就去ip138里面查
				 */
				Map<String, String> headerMap = new HashMap<String, String>();
				JSONObject responseJSON = this.jsoupUtil.ipLocation(ip, headerMap);
				/* 说明查询到数据了 */
				if(responseJSON != null && "0".equalsIgnoreCase(responseJSON.get("code") + ""))
				{
					JSONObject dataResJson = responseJSON.getJSONObject("data");
					if(!"".equalsIgnoreCase(dataResJson.getString("country_id")))
					{
						condMap.clear();
						condMap.put("pinyin", dataResJson.getString("country_id"));
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysIp.setRegionId(region.getId());
						} 
					}else
					{
						/* 根据名字查询,国家 */
						condMap.clear();
						condMap.put("nameLike", "%" + dataResJson.getString("country") + "%");
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysIp.setRegionId(region.getId());
							dataResJson.put("country_id", region.getPinyin());
						} 
						
					}
					/* 存储Ip */
					sysIp.setName(dataResJson.getString("ip"));
					sysIp.setIpStr(dataResJson.getString("country") + "-" + dataResJson.getString("region") + "-"
							+ dataResJson.getString("city"));
					sysIp.setIsp(dataResJson.getString("isp"));
					sysIp.setResponse(responseJSON.toJSONString());
					sysIp.setStatus(ASysIpEnum.STATUS_ENABLE.getStatus());
					sysIp.setContent(dataResJson.getString("allInfo"));
					sysIp.setPubTime(new Date());
					/* 更新数据 */
					ApiResponse<Object> dbApiResponse = this.systemDbService.updateOneSysIpService(sysIp);
					if(dbApiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
					{
						if(dataMapJava.get("succedCount") != null)
						{
							succedCount = Integer.valueOf(dataMapJava.get("succedCount") + "");
							succedCount ++ ;
							dataMapJava.put("succedCount", succedCount);
						}
					}else
					{
						if(dataMapJava.get("failedCount") != null)
						{
							failedCount = Integer.valueOf(dataMapJava.get("failedCount") + "");
							failedCount ++ ;
							dataMapJava.put("failedCount", failedCount);
						}
					}
					if(dataMapJava.get("totalCount") != null)
					{
						totalCount = Integer.valueOf(dataMapJava.get("totalCount") + "");
						totalCount ++ ;
						dataMapJava.put("totalCount", totalCount);
					}
				}
			}
		}
		/* 处理状态 */
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}

	/**
	 * 批量更新国家
	 */
	@Override
	public ApiResponse<Object> operBatchUpdateCountryRegionService()
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		int totalCount = 0 ; 
		int succedCount = 0 ;
		int failedCount = 0 ; 
		Map<String, Object> dataMapJava = apiResponse.getDataMapJava() ;
		/* 判断条数 */
		dataMapJava.put("totalCount", totalCount);
		dataMapJava.put("succedCount", succedCount);
		dataMapJava.put("failedCount", failedCount);
		apiResponse.setDataMapJava(dataMapJava);
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/* 一次性处理100条 */
		pageInfoUtil.setPageSize(100);
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.clear();
		/* 查询所有状态为禁用的ip, */
		condMap.put("status", ASysIpEnum.STATUS_DISABLE.getStatus() + "");
		this.systemDbService.findCondListSysIpService(pageInfoUtil, condMap);
		/* 循环一页一页的处理 */
		for (int i = 1; i <= pageInfoUtil.getTotalPage(); i++)
		{
			ConstatFinalUtil.SYS_LOGGER.info("批量处理Ip;总条数:{},总页数:{},当前页:{}",
					pageInfoUtil.getTotalRecord(),pageInfoUtil.getTotalPage(),pageInfoUtil.getCurrentPage());
			
			/* 重新查询一次 */
			condMap.clear();
			/* 查询所有状态为禁用的ip, */
			condMap.put("status", ASysIpEnum.STATUS_DISABLE.getStatus() + "");
			ApiResponse<ASysIp> sysIpApiResponse = this.systemDbService.findCondListSysIpService(pageInfoUtil, condMap);
			List<ASysIp> sysIpList = sysIpApiResponse.getDataListJava();
			for (Iterator iterator = sysIpList.iterator(); iterator.hasNext();)
			{
				ASysIp sysIp = (ASysIp) iterator.next();
				String ip = sysIp.getIp() ; 
				/**
				 **  如果数据库没查到 就要把这个ip的数据存到数据库
				 ** 先查询淘宝，如果淘宝没查到那就去ip138里面查
				 */
				Map<String, String> headerMap = new HashMap<String, String>();
				JSONObject responseJSON = this.jsoupUtil.ipLocation(ip, headerMap);
				/* 说明查询到数据了 */
				if(responseJSON != null && "0".equalsIgnoreCase(responseJSON.get("code") + ""))
				{
					JSONObject dataResJson = responseJSON.getJSONObject("data");
					if(!"".equalsIgnoreCase(dataResJson.getString("country_id")))
					{
						condMap.clear();
						condMap.put("pinyin", dataResJson.getString("country_id"));
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysIp.setRegionId(region.getId());
						} 
					}else
					{
						/* 根据名字查询,国家 */
						condMap.clear();
						condMap.put("nameLike", "%" + dataResJson.getString("country") + "%");
						ApiResponse<ARegion> regionApiResponse = this.systemDbService.findOneRegionService(condMap);
						ARegion region = regionApiResponse.getDataOneJava();
						if (region != null)
						{
							sysIp.setRegionId(region.getId());
							dataResJson.put("country_id", region.getPinyin());
						} 
						
					}
					/* 存储Ip */
					sysIp.setName(dataResJson.getString("ip"));
					sysIp.setIpStr(dataResJson.getString("country") + "-" + dataResJson.getString("region") + "-"
							+ dataResJson.getString("city"));
					sysIp.setIsp(dataResJson.getString("isp"));
					sysIp.setResponse(responseJSON.toJSONString());
					sysIp.setStatus(ASysIpEnum.STATUS_ENABLE.getStatus());
					sysIp.setContent(dataResJson.getString("allInfo"));
					sysIp.setPubTime(new Date());
					/* 更新数据 */
					ApiResponse<Object> dbApiResponse = this.systemDbService.updateOneSysIpService(sysIp);
					if(dbApiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
					{
						if(dataMapJava.get("succedCount") != null)
						{
							succedCount = Integer.valueOf(dataMapJava.get("succedCount") + "");
							succedCount ++ ;
							dataMapJava.put("succedCount", succedCount);
						}
					}else
					{
						if(dataMapJava.get("failedCount") != null)
						{
							failedCount = Integer.valueOf(dataMapJava.get("failedCount") + "");
							failedCount ++ ;
							dataMapJava.put("failedCount", failedCount);
						}
					}
					if(dataMapJava.get("totalCount") != null)
					{
						totalCount = Integer.valueOf(dataMapJava.get("totalCount") + "");
						totalCount ++ ;
						dataMapJava.put("totalCount", totalCount);
					}
				}
			}
		}
		/* 处理状态 */
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),
				Collections.EMPTY_MAP);
		return apiResponse;
	}
}